// Package coding_round_1 7_103
// @Title coding_round_1
// @Description
// @Author  == 琳琳也叫王木木 ==  2021/9/4 8:59
// @Create  2021/9/4 8:59
package coding_round_1

func reverseArr(num []int) []int {
	arrLen := len(num)
	for i := 0; i < arrLen/2; i++ {
		num[arrLen-1-i], num[i] = num[i], num[arrLen-1-i]
	}
	return num
}

func ZigzagLevelOrder(root *TreeNode) [][]int {
	if root == nil {
		return nil
	}
	ans := [][]int{}
	queue := []*TreeNode{root}
	for i := 0; len(queue) > 0; i++ {
		size := len(queue)
		tempArr := []int{}
		for j := 0; j < size; j++ {
			var node *TreeNode
			node = queue[0]
			queue = queue[1:]
			tempArr = append(tempArr, node.Val)
			if node.Left != nil {
				queue = append(queue, node.Left)
			}
			if node.Right != nil {
				queue = append(queue, node.Right)
			}
		}
		if i%2 != 0 {
			tempArr = reverseArr(tempArr)
		}
		ans = append(ans, tempArr)
	}
	return ans
}
